package Server;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.util.Duration;


public class LockController {

    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private Label remainingLocksLabel;

    @FXML
    private Label timeBlueLabel;

    @FXML
    private Label timeRedLabel;


    private int remainingLocks = 10;
    private Timeline blueTimeline;
    private Timeline redTimeline;
    
    @FXML
    void initialize() {
        


    }
    
    public void decreaseLockCounter(){
    	remainingLocks --;
    	remainingLocksLabel.setText(""+remainingLocks);
    	if(remainingLocks == 0){
    		try{
    		blueTimeline.stop();
    		redTimeline.stop();
    		}catch(NullPointerException e){
    			System.out.println("Not all Timelines are set");
    		}
    	}
    	
    }
    
    public void resetLockCounter(){
    	remainingLocks = 10;
    	remainingLocksLabel.setText(""+remainingLocks);
    	
    }
    
    public void startBlueTimer(){
    if(blueTimeline != null){
	   blueTimeline.stop();
		}else{

			timeBlueLabel.setText("0");
			blueTimeline = new Timeline();
			blueTimeline.setCycleCount(Timeline.INDEFINITE);
			blueTimeline.getKeyFrames().add(
					new KeyFrame(Duration.millis(10),
							new EventHandler<ActionEvent>() {
								int seconds = 0;

								// KeyFrame event handler
								public void handle(ActionEvent event) {
									seconds++;
									// update timerLabel
									timeBlueLabel.setText("" + seconds);
								}
			}));
			blueTimeline.playFromStart();
		}
    }
    
    public void startRedTimer(){
        if(redTimeline != null){
    	   redTimeline.stop();
    		}else{

    			timeRedLabel.setText("0");
    			redTimeline = new Timeline();
    			redTimeline.setCycleCount(Timeline.INDEFINITE);
    			redTimeline.getKeyFrames().add(
    					new KeyFrame(Duration.millis(10),
    							new EventHandler<ActionEvent>() {
    								int seconds = 0;

    								// KeyFrame event handler
    								public void handle(ActionEvent event) {
    									seconds++;
    									// update timerLabel
    									timeRedLabel.setText("" + seconds);
    								}
    			}));
    			redTimeline.playFromStart();
    		}
    }
    
    public void setTimes(String timeBlue, String timeRed){
    	timeBlueLabel.setText(timeBlue);
    	timeRedLabel.setText(timeRed);
    }

}
